/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2p.raca.Client;

/**
 *
 * @author p
 */
import java.io.*;

import java.net.UnknownHostException;
import java.net.Socket;

public class ClientIssueRequest extends Thread {
    
    String hostName=null;
    int port;
    String Message = null;
    private Socket sock;
    public int size;
    private ClientFileSender CFS;
    public ClientIssueRequest(String hostName, int port, String Message)
    {
        this.port = port;
        this.hostName = hostName;
        this.Message = Message;
    }
    
    @Override
    public void run()
    {
        boolean exit = false;
        sendMessage();
        while (exit == false)
        {
            try {
            if (Message.equals("getLista"))
            {

                         sock = new Socket(hostName, 5051);
                        if (sock.isBound())
                        {                    
                            recieveFile("Lista.txt");
                            sock.close();

                        exit = true;
                        }
                        }
                else if (Message.equals("getListaFile"))
                {
                        
                        sock = new Socket(hostName, 5051);
                        if (sock.isBound())
                        {                    
                            recieveFile("ListaFile.txt");
                            sock.close();

                        exit = true;
                        }
                }else if(Message.substring(0,6).equals("getPFI"))
                {
                    sock = new Socket(hostName, 5051);
                        if (sock.isBound())
                        {                    
                            recieveFile(Message.substring(6,Message.length()) + ".pfi");
                            sock.close();

                        exit = true;             }
                }else if(Message.equals("updateFiles"))
                {
                        Client.LF.updateFileList();
                                          
                        CFS = new ClientFileSender("ListaFile.txt");
                        CFS.start();
                            
                                
                        exit = true;
                       // sock.close();
                }
                else if(Message.substring(0,8).equals("donwload")){
                   sock = new Socket();
                    while (!sock.isBound())
                   {
                       try{
                           sock = new Socket(hostName, 5055);
                       }catch(Exception e)
                       {
                       }
                   }
                        if (sock.isBound())
                        {
                            recieveFile(Message.substring(8,Message.length())+".txt");
                            sock.close();
                        exit = true;
                        }
                }
        } catch (UnknownHostException ex) {
        } catch (IOException ex) {
        }
        }
    }
    private void recieveFile(String name) throws UnknownHostException, IOException
    {
        byte[] buff = new byte[1024];
        int bytesRead=0;
        InputStream is = sock.getInputStream();
        OutputStream os = sock.getOutputStream();
        if(Message.substring(0,8).equals("donwload"))
                name = "shared/"+name;
        File myFile = new File(name);
        if (myFile.exists())
            myFile.delete();
        myFile.createNewFile();
        
        FileOutputStream fos = new FileOutputStream(myFile);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        

        int offset=0;
        long start = System.nanoTime();
        while (offset < size)
        {
                
                bytesRead = is.read(buff, 0, 1024);
                bos.write(buff, 0, bytesRead);
                bos.flush();
                offset =+ bytesRead;
                 
        }
            long elapsedTime = System.nanoTime() - start;
          System.out.println("Tempo gasto para enviar o arquivo: "+ myFile.getName()+ " foi: " + elapsedTime);
      
        bos.close();
        sock.close();
    }

    public void sendMessage()
    {
        try { 
            Socket clientSocket = new Socket();
            
                try{
         clientSocket = new Socket(hostName, port);  
            }catch(Exception e)
                    {
                        
                    }
            
         DataOutputStream toServer = new DataOutputStream(clientSocket.getOutputStream());  
         DataInputStream fromServer = new DataInputStream(clientSocket.getInputStream());
         
         toServer.write(Message.getBytes());  
         
         toServer.flush();
         if (Message.equals("updateFiles"))
         {
             File file = new File("ListaFile.txt");
             toServer.write((int)file.length());
             toServer.flush();
             toServer.flush();
             toServer.flush();
         }else if (Message.substring(0,8).equals("donwload"))
         {
            int sizee = fromServer.read();
         }
         else
         {
            size = fromServer.read();
         }
         toServer.close();
         clientSocket.close();
         
      } catch (Exception e) {  
      }  

    }
    
   
    
}
